//
//  NewsInfoController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/30.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "NewsInfoController.h"
#import "UIViewController+Ext.h"
#import "NewsModels.h"

@interface NewsInfoController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation NewsInfoController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithRed:236/255.0f green:241/255.0f blue:242/255.0f alpha:1.0f]];
    self.navigationItem.title = _newsModels.senderName;
    [self setNavBack];
    
    CGRect scrollFrame = CGRectMake(0, SYSTEM_TOP, self.view.frame.size.width, self.view.frame.size.height);
    _scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
    [self.view addSubview:_scrollView];
    
    [self initContent];
    
    [self setNewsRead];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//根据宽度求高度  content 计算的内容  width 计算的宽度 font字体大小
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font:(UIFont *)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    return rect.size.height;
}
//根据高度度求宽度  text 计算的内容  Height 计算的高度 font字体大小
- (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(UIFont *)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:font}
                                     context:nil];
    return rect.size.width;
}


#pragma mark -
- (void)initContent
{
    CGRect viewFrame = self.view.frame;
    NSString *weekString = [_newsModels getWeekDay];
    CGFloat dateWidth = [self getWidthWithText:weekString
                                        height:20 * SCALING
                                          font:[UIFont systemFontOfSize:12.0f * SCALING]] + 10 *SCALING;
    CGRect dateFrame = CGRectMake((CGRectGetWidth(viewFrame) - dateWidth) /2,
                                  15 * SCALING,
                                  dateWidth,
                                  20 * SCALING);
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:dateFrame];
    [dateLabel setTextColor:[UIColor whiteColor]];
    [dateLabel setBackgroundColor:[UIColor colorWithRed:191/255.0f green:191/255.0f blue:191/255.0f alpha:1.0f]];
    [dateLabel setText:weekString];
    [dateLabel setTextAlignment:NSTextAlignmentCenter];
    [dateLabel setFont:[UIFont systemFontOfSize:12.0f * SCALING]];
    dateLabel.layer.masksToBounds = YES;
    dateLabel.layer.cornerRadius = 5.0f * SCALING;
    [_scrollView addSubview:dateLabel];
    
    CGFloat infoWidth = CGRectGetWidth(viewFrame) - 40 * SCALING;
    CGFloat infoHeight = [self getLabelHeightWithText:_newsModels.messageContent
                                                width:infoWidth - 30 * SCALING
                                                 font:[UIFont systemFontOfSize:14.0f * SCALING]];
    CGRect imageFrame = CGRectMake(20 * SCALING,
                                   CGRectGetMaxY(dateFrame) + 5 * SCALING,
                                   infoWidth,
                                   infoHeight + 30 * SCALING);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
    [imageView setBackgroundColor:[UIColor whiteColor]];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 10.0f;
    [_scrollView addSubview:imageView];
    
    CGRect infoFrame = CGRectMake(CGRectGetMinX(imageFrame)+ 15 * SCALING,
                                  CGRectGetMinY(imageFrame)+ 15 * SCALING,
                                  infoWidth - 30 * SCALING,
                                  infoHeight);
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoFrame];
    [infoLabel setTextColor:[UIColor grayColor]];
    [infoLabel setBackgroundColor:[UIColor clearColor]];
    [infoLabel setText:_newsModels.messageContent];
    [infoLabel setNumberOfLines:10000];
    [infoLabel setFont:[UIFont systemFontOfSize:14.0f * SCALING]];
    [_scrollView addSubview:infoLabel];
    
    CGRect timeFrame = CGRectMake(CGRectGetMinX(imageFrame) + 20 * SCALING,
                                  CGRectGetMaxY(imageFrame) + 10 * SCALING,
                                  100 * SCALING,
                                  20 * SCALING);
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:timeFrame];
    [timeLabel setTextColor:[UIColor grayColor]];
    [timeLabel setBackgroundColor:[UIColor clearColor]];
    [timeLabel setText:[_newsModels getTimeString]];
    [timeLabel setFont:[UIFont systemFontOfSize:10.0f * SCALING]];
    [_scrollView addSubview:timeLabel];
    
    [_scrollView setContentSize:CGSizeMake(CGRectGetWidth(viewFrame), CGRectGetMaxY(timeFrame) + 20 *SCALING)];
}

#pragma mark -
- (void)setNewsRead
{
    [self messageRead:self.newsModels.message_id success:^(id responseObject) {
        //[self showSuccess:[responseObject objectForKey:@"msg"]];
        if ([[responseObject objectForKey:@"code"] integerValue]==1) {
            self.block();
        }
    } failure:^(NSString *error) {
        
    }];
}
#pragma mark 消息标记为已读
- (void)messageRead:(NSString *)message_id
            success:(void (^)(id responseObject))success
            failure:(void (^)(NSString  *error))failure
{
    NSString *postURL = @"/authc/message/read";
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                message_id, @"id", nil];
    if (message_id.length <= 0) {
        parameters = nil;
    }
    [[NetworkEngine sharedClient] POST:postURL
                                  data:parameters
                               success:^(id responseObject) {
                                   if (success) {
                                       success(responseObject);
                                   }
                               }
                               failure:^(NSString *error) {
                                   if (failure) {
                                       failure(error);
                                   }
                               }];
}
@end
